package com.itheima.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entry.Category;
import com.itheima.entry.Dish;
import com.itheima.entry.Setmeal;
import com.itheima.pojo.R;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public R<String> add(@RequestBody SetmealDto setmealDto){
        setmealService.addWith(setmealDto);
       return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> findPage(int page, int pageSize, String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(name),Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(setmealPage,wrapper);

        long total = setmealPage.getTotal();
        List<Setmeal> records = setmealPage.getRecords();
        for (Setmeal setmeal : records) {
            Long categoryId = setmeal.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            setmeal.setCategoryName(byId!=null?byId.getName():"");
        }
        setmealPage.setRecords(records);
        return R.success(setmealPage);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        List<Setmeal> list=null;


        String key ="setmeal_"+setmeal.getCategoryId()+"_"+setmeal.getStatus();
        list = (List<Setmeal>) redisTemplate.opsForValue().get(key);
        if (list!=null){
            return R.success(list);
        }


        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());

        list = setmealService.list(setmealLambdaQueryWrapper);

        redisTemplate.opsForValue().set(key,list,60, TimeUnit.MINUTES);
        return R.success(list);
    }
}
